/+
Library dbapi - Simple database api for the D programming language.

Copyright (C) 2007 Myron Alexander (myron.alexander...gmail.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+/

/+
TODO - Make sure that exceptions are not lost/hidden by exceptions thrown in the
       catch section.
TODO - Think of how to abstract the database from the interface for unit tests.
+/

module dbapi.sqlite3;

import std.string;

import dbapi.internal.sqlite3.connection;

public import dbapi.internal.sqlite3.definitions;

/+
version (DbapiSqlite3UnitTest) {

   import dbapi.internal.sqlite3.test.header;

} else {
   
   import sqlite.sqlite3;
}
+/

//------------------------------------------------------------------------------
// SECTION: Public module global constants
//------------------------------------------------------------------------------

const char[] apilevel      = "2.0";
const int    threadsafety  = 0;
const char[] paramstyle    = "qmark";

/**
 * Request connection to an in-memory database without any temporary file.
 */
const char[] IN_MEMORY_DB  = ":memory:";

/**
 * Request connection to a temporary database that acts like an in-memory
 * database except that the database is created as a temporary file.
 */
const char[] TMP_DB        = "";


//------------------------------------------------------------------------------
// SECTION: Public interface aliases
//------------------------------------------------------------------------------

alias dbapi.internal.sqlite3.connection.createConnection   connect;

alias dbapi.internal.sqlite3.cursor.Cursor                 Cursor;

//------------------------------------------------------------------------------
// SECTION: Public interface functions
//------------------------------------------------------------------------------

/**
 * Process the given text to preserve meaning when included in the SQL
 * statement.
 *
 * When text is included in the SQL statement directly, there are rules that
 * must be observed to ensure that the text retains meaning and that the text
 * does not introduce SQL syntax errors. For this function, single quotes
 * within the text are quoted by doubling such that one quote "'" becomes two
 * "''".
 *
 * Params:
 *
 *  s = text to be escaped.
 *
 * Returns:
 *   String with single quotes escaped with single quotes.
 */
char[] escapeText (char[] s) {
   return std.string.replace (s, "'", "''");
}

unittest {
   char[] rs;

   rs = escapeText ("A string without quotes");
   assert ("A string without quotes" == rs);

   rs = escapeText ("'A string with one starting quote");
   assert ("''A string with one starting quote" == rs);

   rs = escapeText ("A string with one trailing quote'");
   assert ("A string with one trailing quote''" == rs);

   rs = escapeText ("'A string with bracketing quotes'");
   assert ("''A string with bracketing quotes''" == rs);

   rs = escapeText ("A string with ' an embedded quote");
   assert ("A string with '' an embedded quote" == rs);

   rs = escapeText ("A string with '' two embedded quotes");
   assert ("A string with '''' two embedded quotes" == rs);
}

// vim:et:sw=3:ts=3:
